package security.security.login;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import security.entity.User;
import security.repository.UserRepository;

@Component
public class CustomUserDetailsService implements UserDetailsService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) {
        User user = userRepository.findByUsername(username);
        if (user == null) {
            return null;
        }
        return new SecurityUser(user);
    }

    public UserDetails loadUserByWxOpenid(String wxOpenid) {
        User user = userRepository.findByUsername(wxOpenid);
        if (user == null) {
            return null;
        }
        return new SecurityUser(user);
    }
}
